package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import listener.ResizeGlassPaneListener;
import utils.ImageOperationUtils;

/**
 * Glass pane using for resize (draw the preview rect)
 * 
 * @author Hugo
 * 
 */
public class ResizeGlassPane extends KoalaGlassPane {
	private int m_curX, m_curY, m_imageLeftPointX, m_imageLeftPointY;
	private JButton m_confirmButton;
	private JButton m_cancelButton;

	public ResizeGlassPane() {
		super();
		m_curX = 0;
		m_curY = 0;

		m_confirmButton = new JButton(new ImageIcon(this.getClass()
				.getResource("/images/" + "valid.png")));
		m_cancelButton = new JButton(new ImageIcon(this.getClass().getResource(
				"/images/" + "cancel.png")));

		m_confirmButton.setName("confirm");
		m_cancelButton.setName("cancel");

		add(m_confirmButton);
		add(m_cancelButton);

		m_confirmButton.addActionListener(new ResizeGlassPaneListener());
		m_cancelButton.addActionListener(new ResizeGlassPaneListener());

		m_confirmButton.setVisible(true);
		m_cancelButton.setVisible(true);

		this.addMouseMotionListener(new ResizeGlassPaneListener());
	}

	@Override
	public void paintComponent(Graphics g) {
		BufferedImage bImg = new BufferedImage(getWidth(), getHeight(),
				BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = bImg.createGraphics();

		for (int x = 0; x < getWidth(); x++) {
			for (int y = 0; y < getHeight(); y++) {
				bImg.setRGB(x, y, new Color(255, 255, 255, 100).getRGB());
			}
		}

		BufferedImage imageSelected = MainFrame.getInstance().getDrawingPane()
				.getSelectedLayer().getImage();

		g.drawImage(bImg, 0, 0, getWidth(), getHeight(), this);

		g.drawImage(ImageOperationUtils.createOpacityOp(imageSelected, 128),
				m_imageLeftPointX, m_imageLeftPointY, m_curX
						- m_imageLeftPointX, m_curY - m_imageLeftPointY, this);

		g.drawRect(m_imageLeftPointX, m_imageLeftPointY, m_curX
				- m_imageLeftPointX, m_curY - m_imageLeftPointY);
		
        super.paintComponent(g);
	}

	/**
	 * Draw a resize rectangle from the upper left corner to the mouse position <br>
	 * (if it isn't inferior than the upper left corner)
	 * 
	 * @param x
	 * @param y
	 */
	public void drawAResizeRect(int x, int y) {

		m_imageLeftPointX = MainFrame.getInstance().getDrawingPane()
				.getSelectedLayer().getX()
				+ MainFrame.getInstance().getDrawingPane().getX() + 3;

		m_imageLeftPointY = (int) (MainFrame.getInstance().getDrawingPane().getSelectedLayer().getY()
				+ MainFrame.getInstance().getDrawingPane().getY() 
				+ MainFrame.getInstance().getMainPanel().getY()
				+ MainFrame.getInstance().getScrollingPane().getY() + 3);

		System.out.println(m_imageLeftPointY);
		System.out.println(y);
		m_curX = (x > m_imageLeftPointX) ? x : m_imageLeftPointX + 1;
		m_curY = (y > m_imageLeftPointY) ? y : m_imageLeftPointY + 1;

		repaint();
	}

	/**
	 * send the resize dimension set by the user
	 * 
	 * @return
	 */
	public Dimension getResizeDimension() {
		return new Dimension(m_curX - m_imageLeftPointX, m_curY
				- m_imageLeftPointY);
	}
}
